package com.Fen.controller.admin;

import com.Fen.result.Result;
import com.Fen.service.ReportService;
import com.Fen.vo.OrderReportVO;
import com.Fen.vo.SalesTop10ReportVO;
import com.Fen.vo.TurnoverReportVO;
import com.Fen.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

@RestController
@RequestMapping("/admin/report")
@Slf4j
@Api(tags = "报表管理")
public class ReportController {

    @Autowired
    private ReportService reportService;

    /**
     * 营业额报表
     *
     * @return
     */
    @GetMapping("/turnoverStatistics")
    @ApiOperation("指定日期营业额报表")
    public Result<TurnoverReportVO> turnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        begin=begin.plusDays(1);
        end=end.plusDays(1);

        return Result.success(reportService.getTurnoverStatistics(begin, end));
    }

    /**
     * 用户数量统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/userStatistics")
    @ApiOperation("用户数量统计")
    public Result<UserReportVO> userStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        begin=begin.plusDays(1);
        end=end.plusDays(1);

        return Result.success(reportService.getuserStatistics(begin, end));
    }

    /**
     * 订单详细统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/ordersStatistics")
    @ApiOperation("订单详细统计")
    public Result<OrderReportVO> orderStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        begin=begin.plusDays(1);
        end=end.plusDays(1);

        return Result.success(reportService.getorderStatistics(begin, end));
    }

    /**
     * 销量排名
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/top10")
    @ApiOperation("销量排名")
    public Result<SalesTop10ReportVO> topTen(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        begin=begin.plusDays(1);
        end=end.plusDays(1);

        return Result.success(reportService.topTen(begin, end));
    }

    /**
     * 导出报表
     */
    @GetMapping("/export")
    @ApiOperation("导出报表")
    public void export(HttpServletResponse response){

        reportService.export(response);
    }
}
